Exploring Ensemble Methods

In this assignment, we will explore the use of boosting. We will use the pre-implemented gradient boosted trees in GraphLab Create. You will:

  • Use SFrames to do some feature engineering.
  • Train a boosted ensemble of decision-trees (gradient boosted trees) on the LendingClub dataset.
  • Predict whether a loan will default along with prediction probabilities (on a validation set).
  • Evaluate the trained model and compare it with a baseline.
  • Find the most positive and negative glbObsAll using the learned model.
  • Explore how the number of trees influences classification performance.

Let's get started!

Fire up Graphlab Create

In [1]:
import graphlab as gl
print('gl.version: %s' % (gl.version))
gl.canvas.set_target('ipynb')
import math
import string

# my imports
import pandas as pd
import numpy as np
from six.moves import cPickle as pickle
gl.version: 1.8.4
In [2]:
from types import MethodType
def value_counts( self ):
    import pandas as pd
    pdDf = self.to_dataframe()
    for ftr in pdDf.columns:
        print(pdDf[ftr].value_counts())
        
#SFrame.value_counts = MethodType(value_counts, None, SFrame)
#setattr(SFrame, 'value_counts', value_counts)
#setattr(glbObsAll, 'value_counts', value_counts)

Load LendingClub dataset

We will be using the LendingClub data. As discussed earlier, the LendingClub is a peer-to-peer leading company that directly connects borrowers and potential lenders/investors.

Just like we did in previous assignments, we will build a classification model to predict whether or not a loan provided by lending club is likely to default.

Let us start by loading the data.

In [ ]:
 
In [ ]:
#glbObsAll = gl.SFrame('lending-club-data.gl/')

Let's quickly explore what the dataset looks like. First, let's print out the column names to see what features we have in this dataset. We have done this in previous assignments, so we won't belabor this here.

In [ ]:
#glbObsAll.column_names()

Modifying the target column

The target column (label column) of the dataset that we are interested in is called bad_glbObsAll. In this column 1 means a risky (bad) loan 0 means a safe loan.

As in past assignments, in order to make this more intuitive and consistent with the lectures, we reassign the target to be:

  • +1 as a safe loan,
  • -1 as a risky (bad) loan.

We put this in a new column called safe_loan.

In [ ]:
#glbObsAll['safe_loan'] = glbObsAll['bad_glbObsAll'].apply(lambda x : +1 if x==0 else -1)
#glbObsAll = glbObsAll.remove_column('bad_glbObsAll')
In [3]:
glbObsAll = gl.load_sframe('data/module-6-decision-tree-practical-assignment_glbObsAll.gl')
2016-03-16 10:39:55,961 [INFO] graphlab.cython.cy_server, 176: GraphLab Create v1.8.4 started. Logging: /tmp/graphlab_server_1458139193.log
This non-commercial license of GraphLab Create is assigned to bbalaji8@gmail.com and will expire on December 09, 2016. For commercial licensing options, visit https://dato.com/buy/.
In [4]:
print(glbObsAll.shape)
glbObsAll.show()
print(glbObsAll)
(122607, 68)
+---------+-----------+-----------+-------------+-----------------+------------+
|    id   | member_id | loan_amnt | funded_amnt | funded_amnt_inv |    term    |
+---------+-----------+-----------+-------------+-----------------+------------+
| 1077501 |  1296599  |    5000   |     5000    |       4975      |  36 months |
| 1077430 |  1314167  |    2500   |     2500    |       2500      |  60 months |
| 1077175 |  1313524  |    2400   |     2400    |       2400      |  36 months |
| 1076863 |  1277178  |   10000   |    10000    |      10000      |  36 months |
| 1075269 |  1311441  |    5000   |     5000    |       5000      |  36 months |
| 1072053 |  1288686  |    3000   |     3000    |       3000      |  36 months |
| 1071795 |  1306957  |    5600   |     5600    |       5600      |  60 months |
| 1071570 |  1306721  |    5375   |     5375    |       5350      |  60 months |
| 1070078 |  1305201  |    6500   |     6500    |       6500      |  60 months |
| 1069908 |  1305008  |   12000   |    12000    |      12000      |  36 months |
+---------+-----------+-----------+-------------+-----------------+------------+
+----------+-------------+-------+-----------+-----------------------+------------+
| int_rate | installment | grade | sub_grade |       emp_title       | emp_length |
+----------+-------------+-------+-----------+-----------------------+------------+
|  10.65   |    162.87   |   B   |     B2    |                       | 10+ years  |
|  15.27   |    59.83    |   C   |     C4    |         Ryder         |  < 1 year  |
|  15.96   |    84.33    |   C   |     C5    |                       | 10+ years  |
|  13.49   |    339.31   |   C   |     C1    |  AIR RESOURCES BOARD  | 10+ years  |
|   7.9    |    156.46   |   A   |     A4    |  Veolia Transportaton |  3 years   |
|  18.64   |    109.43   |   E   |     E1    |    MKC Accounting     |  9 years   |
|  21.28   |    152.39   |   F   |     F2    |                       |  4 years   |
|  12.69   |    121.45   |   B   |     B5    |       Starbucks       |  < 1 year  |
|  14.65   |    153.45   |   C   |     C3    | Southwest Rural metro |  5 years   |
|  12.69   |    402.54   |   B   |     B5    |          UCLA         | 10+ years  |
+----------+-------------+-------+-----------+-----------------------+------------+
+----------------+------------+-----------------+-----------------+-------------+
| home_ownership | annual_inc |     is_inc_v    |     issue_d     | loan_status |
+----------------+------------+-----------------+-----------------+-------------+
|      RENT      |   24000    |     Verified    | 20111201T000000 |  Fully Paid |
|      RENT      |   30000    | Source Verified | 20111201T000000 | Charged Off |
|      RENT      |   12252    |   Not Verified  | 20111201T000000 |  Fully Paid |
|      RENT      |   49200    | Source Verified | 20111201T000000 |  Fully Paid |
|      RENT      |   36000    | Source Verified | 20111201T000000 |  Fully Paid |
|      RENT      |   48000    | Source Verified | 20111201T000000 |  Fully Paid |
|      OWN       |   40000    | Source Verified | 20111201T000000 | Charged Off |
|      RENT      |   15000    |     Verified    | 20111201T000000 | Charged Off |
|      OWN       |   72000    |   Not Verified  | 20111201T000000 |  Fully Paid |
|      OWN       |   75000    | Source Verified | 20111201T000000 |  Fully Paid |
+----------------+------------+-----------------+-----------------+-------------+
+------------+-------------------------------+-------------------------------+-----+
| pymnt_plan |              url              |              desc             | ... |
+------------+-------------------------------+-------------------------------+-----+
|     n      | https://www.lendingclub.co... |   Borrower added on 12/22/... | ... |
|     n      | https://www.lendingclub.co... |   Borrower added on 12/22/... | ... |
|     n      | https://www.lendingclub.co... |                               | ... |
|     n      | https://www.lendingclub.co... |   Borrower added on 12/21/... | ... |
|     n      | https://www.lendingclub.co... |                               | ... |
|     n      | https://www.lendingclub.co... |   Borrower added on 12/16/... | ... |
|     n      | https://www.lendingclub.co... |   Borrower added on 12/21/... | ... |
|     n      | https://www.lendingclub.co... |   Borrower added on 12/16/... | ... |
|     n      | https://www.lendingclub.co... |   Borrower added on 12/15/... | ... |
|     n      | https://www.lendingclub.co... |                               | ... |
+------------+-------------------------------+-------------------------------+-----+
[122607 rows x 68 columns]
Note: Only the head of the SFrame is printed.
You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.

Selecting features

In this assignment, we will be using a subset of features (categorical and numeric). The features we will be using are described in the code comments below. If you are a finance geek, the LendingClub website has a lot more details about these features.

The features we will be using are described in the code comments below:

In [5]:
#target = 'safe_loan'
target = 'safe_loan'
features = ['grade',                     # grade of the loan (categorical)
            'sub_grade_num',             # sub-grade of the loan as a number from 0 to 1
            'short_emp',                 # one year or less of employment
            'emp_length_num',            # number of years of employment
            'home_ownership',            # home_ownership status: own, mortgage or rent
            'dti',                       # debt to income ratio
            'purpose',                   # the purpose of the loan
            'payment_inc_ratio',         # ratio of the monthly payment to income
            'delinq_2yrs',               # number of delinquincies 
            'delinq_2yrs_zero',          # no delinquincies in last 2 years
            'inq_last_6mths',            # number of creditor inquiries in last 6 months
            'last_delinq_none',          # has borrower had a delinquincy
            'last_major_derog_none',     # has borrower had 90 day or worse rating
            'open_acc',                  # number of open credit accounts
            'pub_rec',                   # number of derogatory public records
            'pub_rec_zero',              # no derogatory public records
            'revol_util',                # percent of available credit being used
            'total_rec_late_fee',        # total late fees received to day
            'int_rate',                  # interest rate of the loan
            'total_rec_int',             # interest received to date
            'annual_inc',                # annual income of borrower
            'funded_amnt',               # amount committed to the loan
            'funded_amnt_inv',           # amount committed by investors for the loan
            'installment',               # monthly payment owed by the borrower
           ]

Skipping observations with missing values

Recall from the lectures that one common approach to coping with missing values is to skip observations that contain missing values.

We run the following code to do so:

In [6]:
glbObsAll, glbObsAll_with_na = glbObsAll[[target] + features].dropna_split()

# Count the number of rows with missing data
num_rows_with_na = glbObsAll_with_na.num_rows()
num_rows = glbObsAll.num_rows()
print 'Dropping %s observations; keeping %s ' % (num_rows_with_na, num_rows)
Dropping 29 observations; keeping 122578 

Fortunately, there are not too many missing values. We are retaining most of the data.

Make sure the classes are balanced

We saw in an earlier assignment that this dataset is also imbalanced. We will undersample the larger class (safe glbObsAll) in order to balance out our dataset. We used seed=1 to make sure everyone gets the same results.

In [ ]:
# safe_loan_raw = glbObsAll[glbObsAll[target] == 1]
# risky_glbObsAll_raw = glbObsAll[glbObsAll[target] == -1]

# # Undersample the safe glbObsAll.
# percentage = len(risky_glbObsAll_raw)/float(len(safe_loan_raw))
# safe_loan = safe_loan_raw.sample(percentage, seed = 1)
# risky_glbObsAll = risky_glbObsAll_raw
# glbObsSmp = risky_glbObsAll.append(safe_loan)

# print "Percentage of safe glbObsAll                 :", len(safe_loan) / float(len(glbObsSmp))
# print "Percentage of risky glbObsAll                :", len(risky_glbObsAll) / float(len(glbObsSmp))
# print "Total number of glbObsAll in our new dataset :", len(glbObsSmp)
In [7]:
glbObsSfe = glbObsAll[glbObsAll[target] == +1]
glbObsRsk = glbObsAll[glbObsAll[target] == -1]

# Since there are less risky loans than safe loans, find the ratio of the sizes
# and use that percentage to undersample the safe loans.
percentage = len(glbObsRsk)/float(len(glbObsSfe))
glbObsSfeSmp = glbObsSfe.sample(percentage, seed = 1)
#risky_glbObsAll = glbObsRsk
glbObsSmp = glbObsRsk.append(glbObsSfeSmp)

print "Percentage of safe  loans                : %.2f%%" % \
        (len(glbObsSfeSmp) * 100.0 / float(len(glbObsSmp)))
print "Percentage of risky loans                : %.2f%%" % \
        (len(glbObsRsk)    * 100.0 / float(len(glbObsSmp)))
print "Total number of loans in our new dataset :", len(glbObsSmp)
Percentage of safe  loans                : 50.22%
Percentage of risky loans                : 49.78%
Total number of loans in our new dataset : 46503

Checkpoint: You should now see that the dataset is balanced (approximately 50-50 safe vs risky glbObsAll).

Note: There are many approaches for dealing with imbalanced data, including some where we modify the learning algorithm. These approaches are beyond the scope of this course, but some of them are reviewed in this paper. For this assignment, we use the simplest possible approach, where we subsample the overly represented class to get a more balanced dataset. In general, and especially when the data is highly imbalanced, we recommend using more advanced methods.

Split data into training and validation sets

We split the data into training data and validation data. We used seed=1 to make sure everyone gets the same results. We will use the validation data to help us select model parameters.

In [8]:
glbObsFit, glbObsOOB = glbObsSmp.random_split(.8, seed=1)
print(glbObsFit.shape)
print(glbObsOOB.shape)
(37219, 25)
(9284, 25)

Gradient boosted tree classifier

Gradient boosted trees are a powerful variant of boosting methods; they have been used to win many Kaggle competitions, and have been widely used in industry. We will explore the predictive power of multiple decision trees as opposed to a single decision tree.

Additional reading: If you are interested in gradient boosted trees, here is some additional reading material:

We will now train models to predict safe_loan using the features above. In this section, we will experiment with training an ensemble of 5 trees. To cap the ensemble classifier at 5 trees, we call the function with max_iterations=5 (recall that each iterations corresponds to adding a tree). We set validation_set=None to make sure everyone gets the same results.

In [9]:
maxIter5e0GBTree = gl.boosted_trees_classifier.create(glbObsFit, validation_set = None, 
                    target = target, features = features, max_iterations = 5)
Boosted trees classifier:
--------------------------------------------------------
Number of examples          : 37219
Number of classes           : 2
Number of feature columns   : 24
Number of unpacked features : 24
+-----------+--------------+-------------------+-------------------+
| Iteration | Elapsed Time | Training-accuracy | Training-log_loss |
+-----------+--------------+-------------------+-------------------+
| 1         | 0.056200     | 0.657541          | 0.657139          |
| 2         | 0.115416     | 0.656976          | 0.636157          |
| 3         | 0.169618     | 0.664983          | 0.623206          |
| 4         | 0.222877     | 0.668476          | 0.613783          |
| 5         | 0.276828     | 0.673339          | 0.606229          |
+-----------+--------------+-------------------+-------------------+

Making predictions

Just like we did in previous sections, let us consider a few positive and negative examples from the validation set. We will do the following:

  • Predict whether or not a loan is likely to default.
  • Predict the probability with which the loan is likely to default.
In [10]:
# Select all positive and negative examples.
glbObsOOBSfe = glbObsOOB[glbObsOOB[target] == +1]
glbObsOOBRsk = glbObsOOB[glbObsOOB[target] == -1]

# Select 2 examples from the validation set for positive & negative loans
smpObsOOBSfe = glbObsOOBSfe[0:2]
smpObsOOBRsk = glbObsOOBRsk[0:2]

# Append the 4 examples into a single dataset
smpObsOOB = smpObsOOBSfe.append(smpObsOOBRsk)
smpObsOOB
Out[10]:
safe_loan grade sub_grade_num short_emp emp_length_num home_ownership dti purpose payment_inc_ratio
1 B 0.2 0 3 MORTGAGE 29.44 credit_card 6.30496
1 B 0.6 1 1 RENT 12.19 credit_card 13.4952
-1 D 0.4 0 3 RENT 13.97 other 2.96736
-1 A 1.0 0 11 MORTGAGE 16.33 debt_consolidation 1.90524
delinq_2yrs delinq_2yrs_zero inq_last_6mths last_delinq_none last_major_derog_none open_acc pub_rec pub_rec_zero
0 1 0 1 1 8 0 1
0 1 0 1 1 8 0 1
3 0 0 0 1 14 0 1
0 1 0 1 1 17 0 1
revol_util total_rec_late_fee int_rate total_rec_int annual_inc funded_amnt funded_amnt_inv installment
93.9 0.0 9.91 823.48 92000 15000 15000 483.38
59.1 0.0 11.71 1622.21 25000 8500 8500 281.15
59.5 0.0 16.77 719.11 50004 5000 5000 123.65
62.1 0.0 8.9 696.99 100000 5000 5000 158.77
[4 rows x 25 columns]

Predicting on sample validation data

For each row in the smpObsOOB, write code to make maxIter5e0GBTree predict whether or not the loan is classified as a safe loan.

Hint: Use the predict method in maxIter5e0GBTree for this.

In [11]:
smpObsOOB[target + '.P'] = maxIter5e0GBTree.predict(smpObsOOB)
print(smpObsOOB[[target, target + '.P']])
+-----------+-------------+
| safe_loan | safe_loan.P |
+-----------+-------------+
|     1     |      1      |
|     1     |      1      |
|     -1    |      -1     |
|     -1    |      1      |
+-----------+-------------+
[4 rows x 2 columns]

Quiz question: What percentage of the predictions on smpObsOOB did maxIter5e0GBTree get correct?

Prediction probabilities

For each row in the smpObsOOB, what is the probability (according maxIter5e0GBTree) of a loan being classified as safe?

Hint: Set output_type='probability' to make probability predictions using maxIter5e0GBTree on smpObsOOB:

In [12]:
smpObsOOB[target + '.PPrby'] = maxIter5e0GBTree.predict(smpObsOOB, output_type = 'probability')
print(smpObsOOB[[target, target + '.P', target + '.PPrby']])
+-----------+-------------+-----------------+
| safe_loan | safe_loan.P | safe_loan.PPrby |
+-----------+-------------+-----------------+
|     1     |      1      |  0.704590559006 |
|     1     |      1      |  0.596340894699 |
|     -1    |      -1     |  0.449251592159 |
|     -1    |      1      |  0.611909985542 |
+-----------+-------------+-----------------+
[4 rows x 3 columns]

In [18]:
print(smpObsOOB[[target + sfx for sfx in ['', '.P', '.PPrby']]])
+-----------+-------------+-----------------+
| safe_loan | safe_loan.P | safe_loan.PPrby |
+-----------+-------------+-----------------+
|     1     |      1      |  0.704590559006 |
|     1     |      1      |  0.596340894699 |
|     -1    |      -1     |  0.449251592159 |
|     -1    |      1      |  0.611909985542 |
+-----------+-------------+-----------------+
[4 rows x 3 columns]

Quiz Question: According to maxIter5e0GBTree, which loan is the least likely to be a safe loan?

Checkpoint: Can you verify that for all the predictions with probability >= 0.5, the model predicted the label +1?

Evaluating the model on the validation data

Recall that the accuracy is defined as follows: $$ \mbox{accuracy} = \frac{\mbox{# correctly classified examples}}{\mbox{# total examples}} $$

Evaluate the accuracy of the maxIter5e0GBTree on the glbObsOOB.

Hint: Use the .evaluate() method in the model.

In [20]:
print(maxIter5e0GBTree.evaluate(glbObsOOB, metric='accuracy'))
{'accuracy': 0.66813873330461}

Calculate the number of false positives made by the model.

In [32]:
glbObsOOBMaxIter5e0GBTreeCnfMtrx = maxIter5e0GBTree.evaluate(glbObsOOB, 
                                                metric='confusion_matrix')['confusion_matrix']
print(glbObsOOBMaxIter5e0GBTreeCnfMtrx)
+--------------+-----------------+-------+
| target_label | predicted_label | count |
+--------------+-----------------+-------+
|      -1      |        1        |  1618 |
|      1       |        -1       |  1463 |
|      -1      |        -1       |  3054 |
|      1       |        1        |  3149 |
+--------------+-----------------+-------+
[4 rows x 3 columns]

Quiz question: What is the number of false positives on the glbObsOOB?

Calculate the number of false negatives made by the model.

In [ ]:
 

Comparison with decision trees

In the earlier assignment, we saw that the prediction accuracy of the decision trees was around 0.64 (rounded). In this assignment, we saw that maxIter5e0GBTree has an accuracy of 0.67 (rounded).

Here, we quantify the benefit of the extra 3% increase in accuracy of maxIter5e0GBTree in comparison with a single decision tree from the original decision tree assignment.

As we explored in the earlier assignment, we calculated the cost of the mistakes made by the model. We again consider the same costs as follows:

  • False negatives: Assume a cost of \$10,000 per false negative.
  • False positives: Assume a cost of \$20,000 per false positive.

Assume that the number of false positives and false negatives for the learned decision tree was

  • False negatives: 1936
  • False positives: 1503

Using the costs defined above and the number of false positives and false negatives for the decision tree, we can calculate the total cost of the mistakes made by the decision tree model as follows:

cost = $10,000 * 1936  + $20,000 * 1503 = $49,420,000

The total cost of the mistakes of the model is $49.42M. That is a lot of money!.

Quiz Question: Using the same costs of the false positives and false negatives, what is the cost of the mistakes made by the boosted tree model (maxIter5e0GBTree) as evaluated on the validation_set?

In [46]:
#type(glbObsOOBMaxIter5e0GBTreeCnfMtrx['confusion_matrix'])
#glbObsOOBMaxIter5e0GBTreeCnfMtrx['predicted_label']
print(type(glbObsOOBMaxIter5e0GBTreeCost))
print(glbObsOOBMaxIter5e0GBTreeCost.size())
print(glbObsOOBMaxIter5e0GBTreeCost[0])
<class 'graphlab.data_structures.sarray.SArray'>
1
46990000
In [48]:
glbObsOOBMaxIter5e0GBTreeCost = 10000 *                                \
    glbObsOOBMaxIter5e0GBTreeCnfMtrx[                                  \
        (glbObsOOBMaxIter5e0GBTreeCnfMtrx['predicted_label'] == -1) &  \
        (glbObsOOBMaxIter5e0GBTreeCnfMtrx[   'target_label'] == +1)    \
                                    ]['count']                         \
                                +                                      \
                                20000 *                                \
    glbObsOOBMaxIter5e0GBTreeCnfMtrx[                                  \
        (glbObsOOBMaxIter5e0GBTreeCnfMtrx['predicted_label'] == +1) &  \
        (glbObsOOBMaxIter5e0GBTreeCnfMtrx[   'target_label'] == -1)    \
                                    ]['count']                         
glbObsOOBMaxIter5e0GBTreeCost = glbObsOOBMaxIter5e0GBTreeCost[0]
print('glbObsOOBMaxIter5e0GBTreeCost: $%.2fMM' % (float(glbObsOOBMaxIter5e0GBTreeCost) / 1e6))    
glbObsOOBMaxIter5e0GBTreeCost: $46.99MM

Reminder: Compare the cost of the mistakes made by the boosted trees model with the decision tree model. The extra 3% improvement in prediction accuracy can translate to several million dollars! And, it was so easy to get by simply boosting our decision trees.

Most positive & negative loans.

In this section, we will find the loans that are most likely to be predicted safe. We can do this in a few steps:

  • Step 1: Use the maxIter5e0GBTree (the model with 5 trees) and make probability predictions for all the loans in the glbObsOOB.
  • Step 2: Similar to what we did in the very first assignment, add the probability predictions as a column called predictions into the glbObsOOB.
  • Step 3: Sort the data (in descreasing order) by the probability predictions.

Start here with Step 1 & Step 2. Make predictions using maxIter5e0GBTree for examples in the glbObsOOB. Use output_type = probability.

In [49]:
glbObsOOB[target + '.P']     = maxIter5e0GBTree.predict(glbObsOOB)
glbObsOOB[target + '.PPrby'] = maxIter5e0GBTree.predict(glbObsOOB, output_type = 'probability')
print(glbObsOOB[[target + sfx for sfx in ['', '.P', '.PPrby']]])
+-----------+-------------+-----------------+
| safe_loan | safe_loan.P | safe_loan.PPrby |
+-----------+-------------+-----------------+
|     -1    |      -1     |  0.449251592159 |
|     -1    |      1      |  0.611909985542 |
|     -1    |      -1     |  0.38359811902  |
|     -1    |      -1     |  0.369330704212 |
|     -1    |      1      |  0.59470230341  |
|     -1    |      -1     |  0.337754637003 |
|     -1    |      1      |  0.58944195509  |
|     -1    |      1      |   0.5446626544  |
|     -1    |      -1     |  0.342936486006 |
|     -1    |      -1     |  0.410040646791 |
+-----------+-------------+-----------------+
[9284 rows x 3 columns]
Note: Only the head of the SFrame is printed.
You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.

Checkpoint: For each row, the probabilities should be a number in the range [0, 1]. We have provided a simple check here to make sure your answers are correct.

In [51]:
print "    Your answer : %s\n" % glbObsOOB[target + '.PPrby'].head(4)
print "Expected answer : %s" % [0.4492515948736132, 0.6119100103640573,
                                0.3835981314851436, 0.3693306705994325]
    Your answer : [0.44925159215927124, 0.6119099855422974, 0.38359811902046204, 0.3693307042121887]

Expected answer : [0.4492515948736132, 0.6119100103640573, 0.3835981314851436, 0.3693306705994325]

Now, we are ready to go to Step 3. You can now use the prediction column to sort the glbObsAll in glbObsOOB (in descending order) by prediction probability. Find the top 5 glbObsAll with the highest probability of being predicted as a safe loan.

In [52]:
print(glbObsOOB.topk(target + '.PPrby', 5)[[target + '.PPrby', 'grade']])
+-----------------+-------+
| safe_loan.PPrby | grade |
+-----------------+-------+
|  0.848508358002 |   A   |
|  0.848508358002 |   A   |
|  0.841295421124 |   A   |
|  0.841295421124 |   A   |
|  0.841295421124 |   A   |
+-----------------+-------+
[5 rows x 2 columns]

Quiz question: What grades are the top 5 glbObsAll?

Let us repeat this excercise to find the top 5 glbObsAll (in the glbObsOOB) with the lowest probability of being predicted as a safe loan:

In [54]:
print(glbObsOOB.topk(target + '.PPrby', 5, reverse = True)[[target + '.PPrby', 'grade']])
+-----------------+-------+
| safe_loan.PPrby | grade |
+-----------------+-------+
|  0.134275108576 |   D   |
|  0.134275108576 |   C   |
|  0.134275108576 |   C   |
|  0.134275108576 |   B   |
|  0.134275108576 |   C   |
+-----------------+-------+
[5 rows x 2 columns]

Checkpoint: You should expect to see 5 glbObsAll with the grade ['D', 'C', 'C', 'C', 'B'].

Effect of adding more trees

In this assignment, we will train 5 different ensemble classifiers in the form of gradient boosted trees. We will train models with 10, 50, 100, 200, and 500 trees. We use the max_iterations parameter in the boosted tree module.

Let's get sarted with a model with max_iterations = 10:

In [62]:
maxIter1e1GBTree = gl.boosted_trees_classifier.create(glbObsFit, validation_set=None, 
        target = target, features = features, max_iterations = 10, verbose=False)

Now, train 4 models with max_iterations to be:

  • max_iterations = 50,
  • max_iterations = 100
  • max_iterations = 200
  • max_iterations = 500.

Let us call these models maxIter5e1GBTree, maxIter1e2GBTree, maxIter2e2GBTree, and maxIter5e2GBTree. You can pass in verbose=False in order to suppress the printed output.

Warning: This could take a couple of minutes to run.

In [55]:
maxIter5e1GBTree = gl.boosted_trees_classifier.create(glbObsFit, validation_set = None, 
                    target = target, features = features, max_iterations = 50)
Boosted trees classifier:
--------------------------------------------------------
Number of examples          : 37219
Number of classes           : 2
Number of feature columns   : 24
Number of unpacked features : 24
+-----------+--------------+-------------------+-------------------+
| Iteration | Elapsed Time | Training-accuracy | Training-log_loss |
+-----------+--------------+-------------------+-------------------+
| 1         | 0.055540     | 0.657541          | 0.657139          |
| 2         | 0.108674     | 0.656976          | 0.636157          |
| 3         | 0.163095     | 0.664983          | 0.623206          |
| 4         | 0.218684     | 0.668476          | 0.613783          |
| 5         | 0.275666     | 0.673339          | 0.606229          |
| 6         | 0.328185     | 0.677262          | 0.600809          |
| 11        | 0.576543     | 0.690131          | 0.582282          |
| 25        | 1.292693     | 0.724334          | 0.544715          |
| 50        | 2.594019     | 0.753943          | 0.507820          |
+-----------+--------------+-------------------+-------------------+
In [56]:
maxIter1e2GBTree = gl.boosted_trees_classifier.create(glbObsFit, validation_set = None, 
                    target = target, features = features, max_iterations = 100)
Boosted trees classifier:
--------------------------------------------------------
Number of examples          : 37219
Number of classes           : 2
Number of feature columns   : 24
Number of unpacked features : 24
+-----------+--------------+-------------------+-------------------+
| Iteration | Elapsed Time | Training-accuracy | Training-log_loss |
+-----------+--------------+-------------------+-------------------+
| 1         | 0.056663     | 0.657541          | 0.657139          |
| 2         | 0.115444     | 0.656976          | 0.636157          |
| 3         | 0.168431     | 0.664983          | 0.623206          |
| 4         | 0.221055     | 0.668476          | 0.613783          |
| 5         | 0.280731     | 0.673339          | 0.606229          |
| 6         | 0.335716     | 0.677262          | 0.600809          |
| 11        | 0.588144     | 0.690131          | 0.582282          |
| 25        | 1.331803     | 0.724334          | 0.544715          |
| 50        | 2.659274     | 0.753943          | 0.507820          |
| 51        | 2.712665     | 0.755501          | 0.506300          |
| 75        | 3.942576     | 0.777963          | 0.478113          |
| 100       | 5.206432     | 0.799565          | 0.452256          |
+-----------+--------------+-------------------+-------------------+
In [57]:
maxIter2e2GBTree = gl.boosted_trees_classifier.create(glbObsFit, validation_set = None, 
                    target = target, features = features, max_iterations = 200)
Boosted trees classifier:
--------------------------------------------------------
Number of examples          : 37219
Number of classes           : 2
Number of feature columns   : 24
Number of unpacked features : 24
+-----------+--------------+-------------------+-------------------+
| Iteration | Elapsed Time | Training-accuracy | Training-log_loss |
+-----------+--------------+-------------------+-------------------+
| 1         | 0.054063     | 0.657541          | 0.657139          |
| 2         | 0.111217     | 0.656976          | 0.636157          |
| 3         | 0.164352     | 0.664983          | 0.623206          |
| 4         | 0.219215     | 0.668476          | 0.613783          |
| 5         | 0.276571     | 0.673339          | 0.606229          |
| 6         | 0.330791     | 0.677262          | 0.600809          |
| 11        | 0.581696     | 0.690131          | 0.582282          |
| 25        | 1.302023     | 0.724334          | 0.544715          |
| 50        | 2.576601     | 0.753943          | 0.507820          |
| 51        | 2.629716     | 0.755501          | 0.506300          |
| 75        | 3.945150     | 0.777963          | 0.478113          |
| 100       | 5.511534     | 0.799565          | 0.452256          |
| 101       | 5.579016     | 0.799860          | 0.451931          |
| 125       | 7.199046     | 0.820280          | 0.425868          |
| 150       | 8.644500     | 0.834628          | 0.406161          |
| 175       | 10.095208    | 0.850748          | 0.386342          |
| 200       | 11.533480    | 0.863564          | 0.369538          |
+-----------+--------------+-------------------+-------------------+
In [58]:
maxIter5e2GBTree = gl.boosted_trees_classifier.create(glbObsFit, validation_set = None, 
                    target = target, features = features, max_iterations = 500)
Boosted trees classifier:
--------------------------------------------------------
Number of examples          : 37219
Number of classes           : 2
Number of feature columns   : 24
Number of unpacked features : 24
+-----------+--------------+-------------------+-------------------+
| Iteration | Elapsed Time | Training-accuracy | Training-log_loss |
+-----------+--------------+-------------------+-------------------+
| 1         | 0.055738     | 0.657541          | 0.657139          |
| 2         | 0.110200     | 0.656976          | 0.636157          |
| 3         | 0.165521     | 0.664983          | 0.623206          |
| 4         | 0.220068     | 0.668476          | 0.613783          |
| 5         | 0.276922     | 0.673339          | 0.606229          |
| 6         | 0.331392     | 0.677262          | 0.600809          |
| 11        | 0.580140     | 0.690131          | 0.582282          |
| 25        | 1.318577     | 0.724334          | 0.544715          |
| 50        | 2.611786     | 0.753943          | 0.507820          |
| 51        | 2.665912     | 0.755501          | 0.506300          |
| 75        | 3.875694     | 0.777963          | 0.478113          |
| 100       | 5.187941     | 0.799565          | 0.452256          |
| 101       | 5.236546     | 0.799860          | 0.451931          |
| 125       | 6.474793     | 0.820280          | 0.425868          |
| 150       | 7.766034     | 0.834628          | 0.406161          |
| 175       | 9.044326     | 0.850748          | 0.386342          |
| 200       | 10.307424    | 0.863564          | 0.369538          |
| 225       | 11.594343    | 0.873505          | 0.354368          |
| 250       | 12.855777    | 0.883769          | 0.340257          |
| 275       | 14.162653    | 0.894597          | 0.323590          |
| 300       | 15.449997    | 0.907010          | 0.307725          |
| 325       | 16.728236    | 0.916575          | 0.295360          |
| 350       | 18.000317    | 0.925146          | 0.281916          |
| 375       | 19.272434    | 0.934093          | 0.268026          |
| 400       | 20.533455    | 0.939413          | 0.257453          |
| 425       | 21.816889    | 0.945968          | 0.246981          |
| 450       | 23.087331    | 0.950267          | 0.239107          |
| 475       | 24.362689    | 0.956635          | 0.227256          |
| 500       | 25.647766    | 0.961767          | 0.217013          |
+-----------+--------------+-------------------+-------------------+

Compare accuracy on entire validation set

Now we will compare the predicitve accuracy of our models on the validation set. Evaluate the accuracy of the 10, 50, 100, 200, and 500 tree models on the glbObsOOB. Use the .evaluate method.

In [63]:
print('glbObsOOB 5e0GBTree Accuracy: %.4f' % 
         (maxIter5e0GBTree.evaluate(glbObsOOB, metric='accuracy')['accuracy']))
print('glbObsOOB 1e1GBTree Accuracy: %.4f' % 
         (maxIter1e1GBTree.evaluate(glbObsOOB, metric='accuracy')['accuracy']))
print('glbObsOOB 5e1GBTree Accuracy: %.4f' % 
         (maxIter5e1GBTree.evaluate(glbObsOOB, metric='accuracy')['accuracy']))
print('glbObsOOB 1e2GBTree Accuracy: %.4f' % 
         (maxIter1e2GBTree.evaluate(glbObsOOB, metric='accuracy')['accuracy']))
print('glbObsOOB 2e2GBTree Accuracy: %.4f' % 
         (maxIter2e2GBTree.evaluate(glbObsOOB, metric='accuracy')['accuracy']))
print('glbObsOOB 5e2GBTree Accuracy: %.4f' % 
         (maxIter5e2GBTree.evaluate(glbObsOOB, metric='accuracy')['accuracy']))
glbObsOOB 5e0GBTree Accuracy: 0.6681
glbObsOOB 1e1GBTree Accuracy: 0.6728
glbObsOOB 5e1GBTree Accuracy: 0.6908
glbObsOOB 1e2GBTree Accuracy: 0.6917
glbObsOOB 2e2GBTree Accuracy: 0.6845
glbObsOOB 5e2GBTree Accuracy: 0.6718

Quiz Question: Which model has the best accuracy on the glbObsOOB?

Quiz Question: Is it always true that the model with the most trees will perform best on test data?

Plot the training and validation error vs. number of trees

Recall from the lecture that the classification error is defined as

$$ \mbox{classification error} = 1 - \mbox{accuracy} $$

In this section, we will plot the training and validation errors versus the number of trees to get a sense of how these models are performing. We will compare the 10, 50, 100, 200, and 500 tree models. You will need matplotlib in order to visualize the plots.

First, make sure this block of code runs on your computer.

In [64]:
import matplotlib.pyplot as plt
%matplotlib inline
def make_figure(dim, title, xlabel, ylabel, legend):
    plt.rcParams['figure.figsize'] = dim
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    if legend is not None:
        plt.legend(loc=legend, prop={'size':15})
    plt.rcParams.update({'font.size': 16})
    plt.tight_layout()

In order to plot the classification errors (on the glbObsFit and glbObsOOB) versus the number of trees, we will need lists of these accuracies, which we get by applying the method .evaluate.

Steps to follow:

  • Step 1: Calculate the classification error for model on the training data (glbObsFit).
  • Step 2: Store the training errors into a list (called glbObsFitErr) that looks like this:
    [train_err_10, train_err_50, ..., train_err_500]
  • Step 3: Calculate the classification error of each model on the validation data (glbObsOOB).
  • Step 4: Store the validation classification error into a list (called glbObsOOBErr) that looks like this:
    [validation_err_10, validation_err_50, ..., validation_err_500]
    Once that has been completed, the rest of the code should be able to evaluate correctly and generate the plot.

Let us start with Step 1. Write code to compute the classification error on the glbObsFit for models maxIter1e1GBTree, maxIter5e1GBTree, maxIter1e2GBTree, maxIter2e2GBTree, and maxIter5e2GBTree.

In [65]:
glbObsFitErr = [1 - mdl.evaluate(glbObsFit, metric='accuracy')['accuracy'] \
                    for mdl in [    maxIter5e0GBTree,
                                    maxIter1e1GBTree,
                                    maxIter5e1GBTree,
                                    maxIter1e2GBTree,
                                    maxIter2e2GBTree,
                                    maxIter5e2GBTree
                                ]]
print(glbObsFitErr)
[0.32666111394717756, 0.31174937531905744, 0.24605712136274482, 0.20043526155995595, 0.13643569144791634, 0.03823316048254921]

Now, let us run Step 2. Save the training errors into a list called glbObsFitErr

In [ ]:
# glbObsFitErr = [train_err_10, train_err_50, train_err_100, 
#                    train_err_200, train_err_500]

Now, onto Step 3. Write code to compute the classification error on the glbObsOOB for models maxIter1e1GBTree, maxIter5e1GBTree, maxIter1e2GBTree, maxIter2e2GBTree, and maxIter5e2GBTree.

In [66]:
glbObsOOBErr = [1 - mdl.evaluate(glbObsOOB, metric='accuracy')['accuracy'] \
                    for mdl in [    maxIter5e0GBTree,
                                    maxIter1e1GBTree,
                                    maxIter5e1GBTree,
                                    maxIter1e2GBTree,
                                    maxIter2e2GBTree,
                                    maxIter5e2GBTree
                                ]]
print(glbObsOOBErr)
[0.33186126669538996, 0.3272296423955192, 0.30924170616113744, 0.30827229642395515, 0.31548901335631196, 0.3281990521327014]

Now, let us run Step 4. Save the training errors into a list called glbObsOOBErr

In [ ]:
# glbObsOOBErr = [validation_err_10, validation_err_50, validation_err_100, 
#                      validation_err_200, validation_err_500]

Now, we will plot the glbObsFitErr and glbObsOOBErr versus the number of trees. We will compare the 10, 50, 100, 200, and 500 tree models. We provide some plotting code to visualize the plots within this notebook.

Run the following code to visualize the plots.

In [67]:
plt.plot([5, 10, 50, 100, 200, 500], glbObsFitErr, linewidth=4.0, label='Training error')
plt.plot([5, 10, 50, 100, 200, 500], glbObsOOBErr, linewidth=4.0, label='Validation error')

make_figure(dim=(10,5), title='Error vs number of trees',
            xlabel='Number of trees',
            ylabel='Classification error',
            legend='best')

Quiz question: Does the training error reduce as the number of trees increases?

Quiz question: Is it always true that the validation error will reduce as the number of trees increases?